﻿using System;
using System.Web.Security;
using System.Collections.Generic;
using VASJ.Service.Library.Manager;
using VASJ.Service.Library;

namespace VASJ.Service
{
    public partial class AcpInetService
    {
        #region "AHSMembershipProvider"
        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// ChangePassword
        /// </summary>
        /// <param name="username"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            return Membership.Provider.ChangePassword(username, oldPassword, newPassword);            
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// ChangePasswordQuestionAndAnswer
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="newPasswordQuestion"></param>
        /// <param name="newPasswordAnswer"></param>
        /// <returns></returns>
        public bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            return Membership.Provider.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// CreateUser
        /// </summary>
        /// <param name="projectType"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="passwordQuestion"></param>
        /// <param name="passwordAnswer"></param>
        /// <param name="isApproved"></param>
        /// <param name="providerUserKey"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public MembershipUser CreateUser(string projectType, string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            return new UserManager(projectType).CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// DeleteUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="deleteAllRelatedData"></param>
        /// <returns></returns>
        public bool DeleteUser(string ApplicationName, string username, bool deleteAllRelatedData)
        {
            return new UserManager(ApplicationName).DeleteUser(username, deleteAllRelatedData);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// FindUsersByEmail
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="emailToMatch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public List<MembershipUser> FindUsersByEmail(string ApplicationName, string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            List<MembershipUser> lst = new List<MembershipUser>();

            var lst_user = (new Common(ApplicationName)).GetListUserNameInApplication();

            foreach (MembershipUser mu in Membership.FindUsersByEmail(emailToMatch, pageIndex, pageSize, out totalRecords))
            {
                if (lst_user.Contains(mu.UserName.ToLower())) lst.Add(mu);
            }

            return lst;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]        
        /// <summary>
        /// FindUsersByName
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="usernameToMatch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public List<MembershipUser> FindUsersByName(string ApplicationName, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            List<MembershipUser> lst = new List<MembershipUser>();

            var lst_user = (new Common(ApplicationName)).GetListUserNameInApplication();

            foreach (MembershipUser mu in Membership.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords))
            {
                if (lst_user.Contains(mu.UserName.ToLower())) lst.Add(mu);
            }

            return lst;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]        
        /// <summary>
        /// GetAllUsers
        /// </summary>
        /// <param name="projectType"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public List<MembershipUser> GetAllUsers(string projectType, int pageIndex, int pageSize, out int totalRecords)
        {
            List<MembershipUser> lst = new List<MembershipUser>();

            // Get user in projecttype
            List<string> list_uip = (new Common(projectType)).GetListUserNameInApplication();

            foreach (var it in list_uip)
            {
                lst.Add(Membership.GetUser(it));
            }

            totalRecords = list_uip.Count;

            return lst;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]        
        /// <summary>
        /// GetNumberOfUsersOnline
        /// </summary>
        /// <returns></returns>
        public int GetNumberOfUsersOnline()
        {
            return Membership.GetNumberOfUsersOnline();
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]        
        /// <summary>
        /// GetPassword
        /// </summary>
        /// <param name="username"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public string GetPassword(string username, string answer)
        {
            return Membership.Provider.GetPassword(username, answer);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]        
        /// <summary>
        /// GetUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="userIsOnline"></param>
        /// <returns></returns>
        public MembershipUser GetUser(string ApplicationName, string username, bool userIsOnline)
        {
            return new UserManager(ApplicationName).GetUser(username, userIsOnline);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]        
        /// <summary>
        /// GetUserByProviderUserKey
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="providerUserKey"></param>
        /// <param name="userIsOnline"></param>
        /// <returns></returns>
        public MembershipUser GetUserByProviderUserKey(string ApplicationName, object providerUserKey, bool userIsOnline)
        {
            UserManager um = new UserManager(ApplicationName);

            if(um.IsExistUser(Guid.Parse(providerUserKey.ToString()))) return Membership.GetUser(providerUserKey, userIsOnline); else return null;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]        
        /// <summary>
        /// GetUserNameByEmail
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public string GetUserNameByEmail(string ApplicationName, string email)
        {
            UserManager um = new UserManager(ApplicationName);

            if (um.IsExistUserByEmail(email)) return Membership.GetUserNameByEmail(email); else return null;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// ResetPassword
        /// </summary>
        /// <param name="username"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public string ResetPassword(string username, string answer)
        {
            return Membership.Provider.ResetPassword(username, answer);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// UnlockUser
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool UnlockUser(string userName)
        {
            return Membership.Provider.UnlockUser(userName);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// UpdateUser
        /// </summary>
        /// <param name="user"></param>
        public void UpdateUser(MembershipUser user)
        {
            Membership.UpdateUser(user);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// ValidateUser
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ValidateUser(string username, string password)
        {
            return Membership.Provider.ValidateUser(username, password);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GeneratePassword
        /// </summary>
        /// <param name="length"></param>
        /// <param name="numberOfNonAlphanumericCharacters"></param>
        /// <returns></returns>
        public string GeneratePassword(int length, int numberOfNonAlphanumericCharacters)
        {
            return Membership.GeneratePassword(length, numberOfNonAlphanumericCharacters);
        }
        #endregion
    }
}